package com.example.weatherdemo.slice;

import com.example.weatherdemo.NetWork;
import com.example.weatherdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainAbilitySlice extends AbilitySlice {
    NetWork netWork =null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        netWork = new NetWork(this);
        netWork.callback = new NetWork.NetWorkResult() {
            @Override
            public void onReceiveResult(String content) {
                handlerData(content);
            }
        };
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("Content-Type","application/x-www-form-urlencoded");
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("key", "cffe158caf3fe63aa2959767a503bbfe");
        params.put("city", "上海");
        netWork.startRequest("https://apis.juhe.cn/simpleWeather/query", header, params);
    }

    private void handlerData(String content) {
        Text t1 = findComponentById(ResourceTable.Id_city);
        t1.setText("上海");

        Text t2 = findComponentById(ResourceTable.Id_current_weather);
        Text t3 = findComponentById(ResourceTable.Id_current_aqi);
        Text t4 = findComponentById(ResourceTable.Id_current_dir);
        Text t5 = findComponentById(ResourceTable.Id_current_hum);
        Text t6 = findComponentById(ResourceTable.Id_current_pow);
        Text t7 = findComponentById(ResourceTable.Id_current_temp);

        ZSONObject obj = ZSONObject.stringToZSON(content);
        ZSONObject result = obj.getZSONObject("result");
        ZSONObject real = result.getZSONObject("realtime");
        t2.setText(real.getString("info"));
        t3.setText("空气质量指数：" + real.getString("aqi"));
        t4.setText(real.getString("direct"));
        t5.setText("湿度：" + real.getString("humidity"));
        t6.setText(real.getString("power"));
        t7.setText(real.getString("temperature") + "℃");

        ZSONArray future = result.getZSONArray("future");

        ArrayList<WeatherItem> list = new ArrayList<>();
        for (int i = 0; i < future.size(); i++) {
            WeatherItem item = new WeatherItem();
            ZSONObject f = (ZSONObject) future.get(i);
            item.setDate(f.getString("date"));
            item.setDirect(f.getString("direct"));
            item.setTemperature(f.getString("temperature"));
            item.setWeather(f.getString("weather"));
            list.add(item);
        }

        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_view);
        WeatherListDataProvider sampleItemProvider = new WeatherListDataProvider(this, list);
        listContainer.setItemProvider(sampleItemProvider);
    }
}
